
<template>
  <el-form :model="form" :rules="rule" :ref="`${refName}_form`">
    <el-form-item prop="userName">
      <input
        style="width: 100%"
        placeholder="用户名"
        class="input"
        v-model="form.userName"
      />
    </el-form-item>
    <el-form-item prop="password">
      <div flex="main:justify">
        <input
        type="password"
          class="input"
          placeholder="请输入密码"
          style="width: 100%"
          v-model="form.password"
        />
      </div>
    </el-form-item>
    <el-form-item prop="passwordTwo">
      <div flex="main:justify">
        <input
        type="password"
          class="input"
          placeholder="确认密码"
          style="width: 100%"
          v-model="form.passwordTwo"
        />
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
import { setAccount } from "/@/api/user";
export default {
  props: {
    refName: String,
  },
  data() {
    return {
      form: {
        userName: "",
        password: "",
        passwordTwo: "",
      },
      rule: {
        userName: [{ required: true, message: "请输入用户名" }],
        password: [{ validator: this.validatePass, trigger: "blur" }],
        passwordTwo: [{ validator: this.validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    validatePass(rule, value, callback) {
       let ref = `${this.refName}_form`;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.password !== "") {
          this.$refs[ref].validateField("checkPass");
        }
        callback();
      }
    },
    validatePass2(rule, value, callback) {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    },
    submit() {
      let ref = `${this.refName}_form`;
      return new Promise((reslove, reject) => {
        this.$refs[ref].validate((valid) => {
          if (valid) {
            setAccount({userName:this.form.userName,password:this.form.password})
              .then(() => {
                reslove();
              })
              .catch(() => {
                reject();
              });
          }
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.input {
  height: 40px;
  line-height: 40px;
  display: block;
  border-radius: 20px;
  border: 1px solid #e6e6e6;
  color: #808080;
}
.input::placeholder,
.input {
  padding: 0 20px;
  font-size: 16px;
}
</style>